package com.dimples.dd.system.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.dimples.dd.common.constant.DDServerConstant;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.common.result.CommonResult;
import com.dimples.dd.common.util.object.BeanUtils;
import com.dimples.dd.ip.Area;
import com.dimples.dd.ip.core.utils.AreaUtils;
import com.dimples.dd.ip.core.utils.IPUtils;
import com.dimples.dd.log.core.annotation.ApiLog;
import com.dimples.dd.log.core.enums.OperateTypeEnum;
import com.dimples.dd.system.model.vo.AreaNodeRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "地区")
@RestController
@RequestMapping("/area")
@Validated
public class AreaController {

    @GetMapping("/tree")
    @Operation(summary = "获得地区树")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public PageResult<AreaNodeRespVO> selectAreaTree(@Parameter(description = "地区ID") Integer id) {
        if (ObjectUtil.isEmpty(id)) {
            id = Area.ID_CHINA;
        }
        Area area = AreaUtils.getArea(id);
        Assert.notNull(area, "获取不到中国");
        PageResult<AreaNodeRespVO> pageResult = PageResult.success(CollUtil.newArrayList(BeanUtils.toBean(area.getChildren(), AreaNodeRespVO.class)));
        pageResult.getData().setSize(50L);
        return pageResult;
    }

    @GetMapping("/prov/options")
    @Operation(summary = "获得省级下拉选项")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public CommonResult<List<AreaNodeRespVO>> selectProvAreaList() {
        Area area = AreaUtils.getArea(Area.ID_CHINA);
        Assert.notNull(area, "获取不到中国");
        List<AreaNodeRespVO> areaNodeList = BeanUtils.toBean(area.getChildren(), AreaNodeRespVO.class);
        areaNodeList.forEach(areaNode -> areaNode.setChildren(null));
        return CommonResult.success(areaNodeList);
    }

    @GetMapping("/fetch-by-ip")
    @Operation(summary = "获得 IP 对应的地区名")
    @Parameter(name = "ip", description = "IP, 多个以,分隔", required = true)
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public CommonResult<String> getAreaByIp(@RequestParam("ip") String ip, @RequestParam(defaultValue = "true") boolean format) {
        List<String> result = CollUtil.newArrayList();
        List<String> ipList = StrUtil.split(ip, StrUtil.COMMA);
        for (String ipStr : ipList) {
            // 获得城市
            Area area = IPUtils.getArea(ipStr);
            if (area == null) {
                if (format) {
                    result.add("【" + ipStr + "】" + "未知");
                } else {
                    result.add("未知");
                }
            } else {
                if (format) {
                    result.add("【" + ipStr + "】" + AreaUtils.format(area.getId()));
                } else {
                    result.add(AreaUtils.format(area.getId()));
                }
            }
        }
        // 格式化返回
        return CommonResult.success(StrUtil.join("\n", result));
    }

}
